import {createWebHistory, createRouter } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useStore } from '../store/index'

const routes: any = [
    { 
        path: '/',
        name: 'home',
        component: () => import('@/view/home/home.vue') 
    },
    {
        path: '/mxcontent',
        name: 'mxcontent',
        component: () => import('@/view/ai/mxcontent.vue')
    },
    {
        path: '/chatwidget',
        name: 'chatwidget',
        component: () => import('@/view/ChatWidget/ChatWidget.vue')
    },
    {
        path: '/:pathMatch',
        name: '404',
        component: () => import('@/view/error/404.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach(to => {
    NProgress.start()
    const token = useStore().token;
    if (!token && to.path !== '/') {
        ElMessage.warning("请先登录");
        return { name: 'home' }
    }
})

router.afterEach(() => {
    NProgress.done()
})

export default router

